#include <mono/common/clock.h>
#include <mono/common/timestamp.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#undef min
#undef max
#include <limits>


namespace mono {


const Clock::ClockVal Clock::kClockValMin = std::numeric_limits<Clock::ClockVal>::min();
const Clock::ClockVal Clock::kClockValMax = std::numeric_limits<Clock::ClockVal>::max();


Clock::Clock()
{
	update();
}


Clock::Clock(ClockVal tv)
{
	_clock = tv;
}


Clock::Clock(const Clock& other)
{
	_clock = other._clock;
}


Clock::~Clock()
{
}


Clock& Clock::operator = (const Clock& other)
{
	_clock = other._clock;
	return *this;
}


Clock& Clock::operator = (ClockVal tv)
{
	_clock = tv;
	return *this;
}


void Clock::swap(Clock& timestamp)
{
	std::swap(_clock, timestamp._clock);
}


void Clock::update()
{
	struct timespec ts;
	::clock_gettime(CLOCK_MONOTONIC, &ts);
	_clock = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;

}


Clock::ClockDiff Clock::accuracy()
{
	struct timespec ts;
	::clock_getres(CLOCK_MONOTONIC, &ts);
	ClockVal acc = ClockVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;
	return acc > 0 ? acc : 1;

}

	
bool Clock::monotonic()
{
	return true;
}


} 
